package com.example.widget.view

import android.os.Bundle
import androidx.compose.foundation.layout.*
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.unit.dp
import com.example.base.BaseActivity
import com.example.base.commonView.CommonText
import com.example.base.commonView.CommonTheme
import com.example.base.commonView.TitleBar
import com.example.widget.R

class NavigationViewActivity : BaseActivity() {
    private val items = arrayListOf("首页", "收藏", "我的")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            showView()
        }
    }

    @Composable
    fun showView() {
        val context = ContextAmbient.current as NavigationViewActivity
        var selectedIndex by remember { mutableStateOf(0) }
        CommonTheme {
            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.SpaceBetween,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                TitleBar(title = "NavigationView") {
                    context.finish()
                }
                CommonText(text = items[selectedIndex])
                BottomNavigation {
                    items.forEachIndexed { index, it ->
                        BottomNavigationItem(
                            icon = {
                                if (selectedIndex == index)
                                    Icon(
                                        asset = imageResource(id = R.mipmap.base_icon_back),
                                        modifier = Modifier.size(20.dp)
                                    )
                                else
                                    Icon(
                                        asset = imageResource(id = R.mipmap.base_icon_arrow),
                                        modifier = Modifier.size(20.dp)
                                    )
                            },
                            label = {
                                Text(text = it)
                            },
                            selected = selectedIndex == index,
                            onClick = { selectedIndex = index }
                        )
                    }
                }
            }
        }
    }
}